package com.ato.MyCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author wangjing
 * @create 2021--11--05--21:39
 */
public class IteratorTest {
    public static void main(String[] args) {

        Collection c1 = new ArrayList();
        c1.add(1);
        c1.add(2);
        c1.add("tom");
        Iterator iterator = c1.iterator();
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        // 超出索引报错 NoSuchElement
//        System.out.println(iterator.next());
//        System.out.println(iterator.hasNext());
//        以上方法和for都不推荐，推荐使用 while
        while (iterator.hasNext()) {
//            System.out.println(iterator.next());
            if ("tom".equals(iterator.next())) {
                iterator.remove();
            }
        }
        Iterator iterator1 = c1.iterator();
        while (iterator1.hasNext()) {
            System.out.println(iterator1.next());
        }

    }
}
